package com.andyliu.system.service.impl;

import com.andyliu.system.dto.UserDto;
import com.andyliu.system.entity.User;
import com.andyliu.system.mapper.UserMapper;
import com.andyliu.system.service.IUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 用户信息表 服务实现类
 * </p>
 *
 * @author andyLiu
 * @since 2021-08-10
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private UserMapper userMapper;

      public  IPage<UserDto> selectByPage(Integer pageNumber, Integer pageSize, User user){
        return userMapper
                .selectByPage(new Page<User>(pageNumber,pageSize),user);
      }

    @Override
    public User getUserByLoginName(String loginName) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
        queryWrapper.eq("login_name",loginName);
        List<User> users = userMapper.selectList(queryWrapper);
        return users.size()==1?users.get(0):null;
    }

    @Override
    public UserDto queryUserById(Integer userId) {
        return userMapper.queryUserById(userId);
    }

    @Override
    public boolean passwordMismatch(String oldPassword,User user) {
          boolean status = false;
          String pwdInDb = user.getPassword();
          SimpleHash hash = new SimpleHash("MD5", oldPassword, user.getLoginName(), 1024);
          String pwd = hash.toString();
          return pwdInDb.equals(pwd)?true:false;
    }
}
